package com.rabbitmq.topic;

import com.rabbitmq.RabbitMqConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    //交换机名称
    static final String TOPIC_EXCHANGE = "topic_exchange";
    //队列名称1
    static final String TOPIC_QUEUE_1 = "topic_queue_1";
    //队列名称2
    static final String TOPIC_QUEUE_2 = "topic_queue_2";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接
        Connection connection = RabbitMqConnectionUtil.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        /**
         * 声明交换机
         * 参数1 交换机名称
         * 参数2 交换机类型，fanout、topic、direct、headers
         */
        channel.exchangeDeclare(TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC);

        //发送消息
        String message = "insert Topic_Mode routing key->item.insert";
        channel.basicPublish(TOPIC_EXCHANGE,"item.insert",null,message.getBytes());
        System.out.println("已发送消息：" +message);

        //发送消息
        message = "update Topic_Mode routing key->item.update";
        channel.basicPublish(TOPIC_EXCHANGE,"item.update",null,message.getBytes());
        System.out.println("已发送消息：" +message);

        //发送消息
        message = "delete Topic_Mode routing key->item.delete";
        channel.basicPublish(TOPIC_EXCHANGE,"item.delete",null,message.getBytes());
        System.out.println("已发送消息：" +message);

        //关闭资源
        channel.close();
        connection.close();
    }
}
